package mi.payment.ui.base;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.widget.TextView;

import mi.payment.R;
import mi.payment.constants.Configs;


/**
 * 输入金额键盘
 *
 * @author longtao.li on 2016/11/19.
 */
public class AmountInputView extends TextView {

    // 缓存输入的密码
    private StringBuilder inputSB;

    private String def = "0.00";

    //是否设置新的输入方式(目前只支持小米通道)
    private boolean isNewInputType = Configs.getInstance().IS_SETTING_NEW_INPUT_TYPE();

    //判断用户是否点击过点号
    private boolean isClickDot = false;

    private int rightIndex = 0;

    public AmountInputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inputSB = new StringBuilder();
        init();
    }

    private void init() {
        inputSB.append(def);
        String str = getResources().getString(R.string.rmb) + def;
        setText(str);
    }

    /**
     * 输入一个字符
     */
    public synchronized void addText(String text) {
        if (isNewInputType) {
            newAddText(text);
        } else {
            if (inputSB.length() >= 10) {
                return;
            }
            inputSB.append(text);
        }
        String str = inputSB.toString();
        String cutStr = filter(str);

        inputSB.setLength(0);
        inputSB.append(cutStr);
        String value = getResources().getString(R.string.rmb) + inputSB.toString();
        setText(value);
        invalidate();
    }

    /**
     * 输入一个字符
     */
    private void newAddText(String text) {
        int length = inputSB.length();
        if (isClickDot) {
            if (rightIndex == 0) {
                inputSB.replace(length - 2, length - 1, text);
            }
            if (rightIndex == 1) {
                if (!text.equals("0")) {
                    inputSB.replace(length - 1, length, text);
                    rightIndex++;
                }
            }
            if (rightIndex == 0) {
                rightIndex++;
            }
        } else {
            if (inputSB.length() >= 10) {
                return;
            }
            if (inputSB.toString().equals(def)) {
                inputSB = inputSB.replace(0, 1, text);
            } else {
                inputSB.insert(length - 3, text);
            }
        }
    }

    @NonNull
    private String filter(String str) {
        String cutStr = str;
        /* 删除字符串中的dot */
        for (int i = str.length() - 1; i >= 0; i--) {
            char c = str.charAt(i);
            if ('.' == c) {
                cutStr = str.substring(0, i) + str.substring(i + 1);
                break;
            }
        }
        /* 删除前面多余的0 */
        int NUM = cutStr.length();
        int zeroIndex = -1;
        for (int i = 0; i < NUM - 2; i++) {
            char c = cutStr.charAt(i);
            if (c != '0') {
                zeroIndex = i;
                break;
            } else if (i == NUM - 3) {
                zeroIndex = i;
                break;
            }
        }
        if (zeroIndex != -1) {
            cutStr = cutStr.substring(zeroIndex);
        }
        /* 不足3位补0 */
        if (cutStr.length() < 3) {
            cutStr = "0" + cutStr;
        }
        /* 加上dot，以显示小数点后两位 */
        cutStr = cutStr.substring(0, cutStr.length() - 2)
                + "." + cutStr.substring(cutStr.length() - 2);
        return cutStr;
    }

    /**
     * 删除最后一个
     */
    public void delLast() {
        int length = inputSB.length();
        if (isNewInputType) {
            newDelLast(length);
        } else {
            if ((inputSB.toString()).equals(def))
                return;
            inputSB.deleteCharAt(length - 1);
        }
        String temp = inputSB.toString();
        temp = filter(temp);
        inputSB.setLength(0);
        inputSB.append(temp);
        String value = getResources().getString(R.string.rmb) + inputSB.toString();
        setText(value);
    }

    /**
     * 删除最后一个
     *
     * @param length
     */
    private void newDelLast(int length) {
        if (isClickDot) {
            if (rightIndex == 2) {
                inputSB.replace(length - 1, length, "0");
            }
            if (rightIndex == 1) {
                inputSB.replace(length - 2, length - 1, "0");
            }
            if (rightIndex == 0) {
                inputSB.deleteCharAt(length - 4);
            }
            if (rightIndex == 1) {
                rightIndex--;
                isClickDot = false;
            }
            if (rightIndex == 2) {
                rightIndex--;
            }
        } else {
            inputSB.deleteCharAt(length - 4);
        }
    }

    /**
     * 清空
     */
    public void clean() {
        inputSB.setLength(0);
        inputSB.append(def);
        String value = getResources().getString(R.string.rmb) + String.valueOf(inputSB);
        setText(value);
        isClickDot = false;
    }

    public CharSequence getAmountText() {
        return inputSB.toString();
    }

    public void setClickDot(boolean clickDot) {
        isClickDot = clickDot;
    }

    public boolean isClickDot() {
        return isClickDot;
    }

    public void setRightIndex(int rightIndex) {
        this.rightIndex = rightIndex;
    }
}
